/*
package com.woniu.shops.loginInterceptor;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.druid.util.StringUtils;
import com.woniu.common.constans.Constants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String token = request.getHeader(Constants.TOKEN);
        if(token == null || token.isEmpty()){
            response.sendRedirect("/login");
            return false;
        }
        //2. 校验token
        JWT jwt = JWTUtil.parseToken(token);
        String username = (String) jwt.getPayload("username");
        // 校验token是否合法，如果有效返回true，否则返回false
        boolean verify = JWTUtil.verify(token, username.getBytes());
        //3. 判断
        if (!verify) {
            return false;
        }
        //4. 校验redis的cookie是否一致
        String tokenInRedis = (String) redisTemplate.opsForValue().get(Constants.TOKEN);
        if (!StringUtils.equals(token, tokenInRedis)) {
            return false;
        }
        return true;
    }
}
*/
